/*
 *  Copyright 2019-2023 oyun
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.oyun.modules.videoandbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.medicine.domain.Vaccine;
import com.oyun.modules.videoandbook.domain.Video;
import com.oyun.modules.videoandbook.domain.vo.VideoQueryCriteria;
import com.oyun.modules.videoandbook.mapper.VideoMapper;
import com.oyun.modules.videoandbook.service.VideoService;
import com.oyun.segment.convert.itmd.IntermediateCodeUtils;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import com.oyun.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author oyun
 * @description 服务实现
 * @date 2024-05-08
 **/
@Service
@RequiredArgsConstructor
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    private final VideoMapper videoMapper;

    @Override
    public PageResult<Video> queryAll(VideoQueryCriteria criteria, Page<Object> page) {
        if (StringUtils.isNotEmpty(criteria.getVideoTitle())) {
            criteria.setReplaceAll(IntermediateCodeUtils.mn2Code(criteria.getVideoTitle()).replaceAll("-", " -"));
        }
        return PageUtil.toPage(videoMapper.findAll(criteria, page));
    }

    @Override
    public List<Video> queryAll(VideoQueryCriteria criteria) {
        return videoMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Video resources) {
        convertCode(resources);
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Video resources) {
        Video video = getById(resources.getId());
        video.copy(resources);
        convertCode(video);
        saveOrUpdate(video);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Video> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Video video : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("视频标题", video.getVideoTitle());
            map.put("视频标题中间码", video.getVideoTitleCode());
            map.put("视频url", video.getVideoUrl());
            map.put("封面图片", video.getCoverPicture());
            map.put("视频类型", video.getVideoType());
            map.put("摘要", video.getAbstrac());
            map.put("目录", video.getCatalogue());
            map.put("创作者", video.getCreator());
            map.put("资源路径", video.getResourcePath());
            map.put("出版商", video.getPublisher());
            map.put("版权", video.getCopyright());
            map.put("位置", video.getPosition());
            map.put(" createBy", video.getCreateBy());
            map.put(" createTime", video.getCreateTime());
            map.put(" updateBy", video.getUpdateBy());
            map.put(" updateTime", video.getUpdateTime());
            map.put(" status", video.getStatus());
            map.put("备注", video.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Object getByName(String name) {
        String replaceAll = IntermediateCodeUtils.mn2Code(name).replaceAll("-", " -");
        LambdaQueryWrapper<Video> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Video::getVideoTitleCode, replaceAll);
        return videoMapper.selectList(wrapper);
    }

    private void convertCode(Video video) {
        video.setVideoTitleCode(IntermediateCodeUtils.mn2Code(video.getVideoTitle()).replaceAll("-", " -"));
    }
}
